$(document).ready(function(){  
//    $('#deco').qtip({
//        content: $('#deco_qtip'),
//        position: {
//                my: 'top center', 
//                at: 'bottom center'
//        },
//        show: {
//            event: 'mouseenter',
//            effect: function() {
//                $(this).stop(0, 1).fadeIn(400);
//            }
//        },
//        hide: {
//            event: 'mouseleave',
//            effect: function() {
//                $(this).stop(0, 1).fadeOut(400);
//            }
//        },
//        style: { 
//                tip: true,
//                classes: 'ui-tooltip-blue ui-tooltip-shadow ui-tooltip-rounded'
//        }
//    });

   
   
   $("#help_cb-enable").click(function(){
       if(!$(this).hasClass("selected")) help(1, $(this));
    });
    
    $("#help_cb-disable").click(function(){
        if(!$(this).hasClass("selected")) help(0, $(this));
    });
    
    
    $('input[qtip],select[qtip],textarea[qtip]').qtip({
	content: {
            attr: 'qtip'
	},
        position: {
            my: 'left center', 
            at: 'right center'
        },
	show: {
            event: '',
            effect: function() {
                $(this).stop(0, 1).fadeIn(400);
            }
	},
        hide: {
            event: '',
            effect: function() {
                $(this).stop(0, 1).fadeOut(400);
            }
        },
        style: { 
                tip: true,
                classes: 'ui-tooltip-blue ui-tooltip-shadow ui-tooltip-rounded'
        }
    });
    
    $('input[qtip],select[qtip],textarea[qtip]').click(function(){
        if(helpIsActivated()) $(this).qtip('show');
    });
    
    $('input[qtip],select[qtip],textarea[qtip]').blur(function(){
        $(this).qtip('hide');
    });
  
});


function help(help, bouton){
    var noty_help = null;
    return $.ajax({
        type:'GET',
        url : "/infosUser/help",
        data : {
            help:help
        },
        dataType: 'json',
        success: function(data){
            if(data.status == "success") switchBoutonOnOff(bouton, data.help);
            noty_help = myNoty(data.msg, data.status);
        },
        error: function(jqXHR, textStatus, errorThrown)
        {
            noty_help = myNoty("Erreur : "+errorThrown, "error");
        }
    });
}




function switchBoutonOnOff(bouton, on)
{
    var parent = bouton.parents('.cb-switch');
    if(on == 1 ) $('.cb-disable',parent).removeClass('selected');
    else $('.cb-enable',parent).removeClass('selected');
    bouton.addClass('selected');
}



function myNoty(text, lvl)
{
    var myNoty = noty({
        layout : 'bottom', // (top, topLeft, topCenter, topRight, bottom, center, bottomLeft, bottomRight)
        theme : 'noty_theme_default',  // theme name (accessable with CSS)
        animateOpen : {height: 'toggle'}, // opening animation
        animateClose : {height: 'toggle'}, // closing animation
        easing : 'swing', // easing
        text : text, // notification text
        type : lvl, // noty type (alert, success, error)
        speed : 500, // opening & closing animation speed
        timeout : 3000, // delay for closing event. Set false for sticky notifications
        closeButton : false, // enables the close button when set to true
        closeOnSelfClick : true, // close the noty on self click when set to true
        closeOnSelfHover : false, // close the noty on self mouseover when set to true
        force : false, // adds notification to the beginning of queue when set to true
        onShow : false, // callback for on show
        onClose : false, // callback for on close
        buttons : false, // an array of buttons
        modal : false, // adds modal layer when set to true
        template: '<div class="noty_message"><span class="noty_text"></span><div class="noty_close"></div></div>',
        cssPrefix: 'noty_', // this variable will be a type and layout prefix.
        custom: {
            container: null // $('.custom_container')
        }
  });
}


function helpIsActivated()
{
    return $("#help_cb-enable").hasClass("selected");
}